/**
 * Changes:
 * -Inherites from IEventServerDAO
 */

package eventReservationServer.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;


public class EventServerDAO implements IEventServerDAO
{
	private Connection connection = null;
	
	@Override
	public void connect() throws ClassNotFoundException, SQLException
	{
		Class.forName("org.sqlite.JDBC");
		connection = java.sql.DriverManager.getConnection("jdbc:sqlite:EventReservationCore/db/rmi-db/reservations.db");
		
		System.out.println("Base de datos: nueva conexión."); // registro	}
	}
	
	@Override
	public int getPlacesCount(String eventServer, String eventName)
	{
		// createTable();
		
		int places = 0;
		
		try
		{
//			connect();
			
			Statement sentencia = createStatement();
			String query = "SELECT COUNT(*) FROM reservations WHERE  [event-server]='" + eventServer + "' AND [event-name]='" + eventName + "'";
			//System.out.println(query);
			ResultSet rs = sentencia.executeQuery(query);
			
			places = rs.getInt(1);
			
			rs.close();
			sentencia.close();
//	  		disconnect();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		//System.out.println("Valor variable Places: " + places);
		
		System.out.println("Base de datos: consultado el número de reservas para un evento."); // registro
		
		return places;
	}

	@Override
	public void reservePlace(String userName, String eventServer,
			String eventName)
	{
		try
		{
//			connect();
			
			Statement sentencia = createStatement();
			String query = "INSERT INTO reservations VALUES('" + userName + "', '" + eventServer + "', '" + eventName + "', '" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()) + "')";
			sentencia.executeUpdate(query);
			
			System.out.println("Base de datos: nueva reserva insertada."); // registro

			sentencia.close();
//	  		disconnect();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}	
	}
	
//	private void createTable() // Necesitamos crear este metodo para crear la tabla
//	{
//		try
//		{
//			connect();
//			
//			Statement sentencia = createStatement();
//			String query = "CREATE TABLE [reservations] (" +
//					"[username] VARCHAR(64)  NOT NULL, [event-server] VARCHAR(64)  NULL, " +
//					"[event-name] VARCHAR(64)  NULL, [reservation-date] TIMESTAMP  NOT NULL, " +
//					"PRIMARY KEY ([username], [reservation-date]))";
//
//			sentencia.executeUpdate(query);
//
//			sentencia.close();
//	  		disconnect();
//		} 
//		catch (ClassNotFoundException e)
//		{
//			e.printStackTrace();
//		} 
//		catch (SQLException e)
//		{
//			e.printStackTrace();
//		}	
//	}

	@Override
	public void disconnect() throws SQLException
	{
		checkConnected();
		connection.close();
		
		System.out.println("Base de datos: conexión cerrada."); // registro
	}

	private void checkConnected()
	{
		if (connection == null)
			throw new IllegalStateException("A�n no se ha creado la conexi�n a la base de datos");		
	}

	private Statement createStatement() throws SQLException
	{
		checkConnected();
		return connection.createStatement();
	}
}
